package com.github.postsanf.yinian.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.umeng.analytics.MobclickAgent;

import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SetNumCodeMM extends BaseActivity {
    private EditText numcodelock_mm1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_numcodemm);
        initView();
    }

    private void initView() {
        Toolbar mToolbar = (Toolbar) findViewById(R.id.numcodemm_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_return);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView numcodelockTip = (TextView) findViewById(R.id.numcodelock_tip);
        numcodelockTip.setText("设置忆年密码");
        numcodelock_mm1 = (EditText) findViewById(R.id.numcodelock_mm1);
        numcodelock_mm1.setFocusableInTouchMode(true);
        numcodelock_mm1.requestFocus();
        new Timer().schedule(new TimerTask() {
                                 public void run() {
                                     InputMethodManager inputManager =
                                             (InputMethodManager) numcodelock_mm1.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                                     inputManager.showSoftInput(numcodelock_mm1, 0);
                                 }
                             },
                998);
        numcodelock_mm1.addTextChangedListener(mTextWatcher);
    }

    TextWatcher mTextWatcher = new TextWatcher() {
        private CharSequence temp;

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            temp = s;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (temp.length() == 4) {
                String etstr = numcodelock_mm1.getText().toString();
                Matcher m = Pattern.compile("[0-9]*").matcher(etstr);
                if (!m.matches()) {
                    showToast("请输入4位数字密码");
                    return;
                }
                Intent mIntent = new Intent(SetNumCodeMM.this, SetNumCodeMMRepeat.class);
                mIntent.putExtra("mm", etstr);
                startActivity(mIntent);
                finish();
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        sendBroadcast(new Intent(ACTIONs.actionSetNumCode));
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
